//
//  TrainTrains.m
//  MINFOBBS
//
//  Created by juvid on 13-11-28.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "TrainTrains.h"
#import "DetectionSystemVersion.h"
#import "UIColor+expanded.h"
#import "SBJson.h"
#import "TrainStation.h"
@interface TrainTrains (){
    int viewHight;
}

@end

@implementation TrainTrains

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (DEVICE_IS_IPHONE5) {
        viewHight = 504;
    }
    
    else {
       viewHight= 416;
    }
    [self BackButton];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    if (self.isSelf) {
        [self GetDatas];
    }
    else{
        [self ShowData];
        self.arrReturn=Nil;
//    cusCell.vieCell.layer.masksToBounds=YES;
        
    }
    self.table.layer.borderWidth = 1;
    self.table.layer.borderColor = [[UIColor colorWithHexString:@"0xdadada"] CGColor];
    self.vieOut.layer.borderWidth = 1.5;
    self.vieOut.layer.cornerRadius=5;
    self.vieOut.layer.borderColor = [[UIColor colorWithHexString:@"0xdadada"] CGColor];
      self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view from its nib.
}
-(void)ShowData{
    NSArray *arr=[[self.arrReturn objectForKey:@"Properties"] objectForKey:@"Property"];
    self.labTrainTimes.text=[[arr objectAtIndex:0] objectForKey:@"text"];
    self.imgTrainClass.image=[UIImage imageNamed:[[arr objectAtIndex:1] objectForKey:@"text"]];
    self.labTrainClass.text=[[arr objectAtIndex:1] objectForKey:@"text"];
    self.labFirst.text=[[arr objectAtIndex:2] objectForKey:@"text"];
    self.labEnd.text=[[arr objectAtIndex:4] objectForKey:@"text"];
    self.labTimeAll.text=[NSString stringWithFormat:@"运行时间：%@",[[arr objectAtIndex:7] objectForKey:@"text"]];
    self.labJuAll.text=[NSString stringWithFormat:@"距离：%@",[[arr objectAtIndex:8] objectForKey:@"text"]];
    self.labTimeFirst.text=[[arr objectAtIndex:3] objectForKey:@"text"];
    self.labTimeEnd.text=[[arr objectAtIndex:5] objectForKey:@"text"];
    arrTick=[[NSMutableArray alloc]init];
    for (int i=9; i<arr.count-1; i++) {
        if (![[[arr objectAtIndex:i] objectForKey:@"text"] isEqual:@""]) {
            [arrTick addObject:[arr objectAtIndex:i]];
        }
        
    }

    int currentHight=0;
    int nextHight=226;
    if (self.isSelf) {
        nextHight=nextHight+30;
        currentHight=44*arrTick.count+nextHight;
        if (currentHight+24>viewHight) {
            currentHight=viewHight-24;
        }
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(110, currentHight-40, 76, 30)];
        btn.backgroundColor=[UIColor redColor];
        [btn setTitle:@"途经站" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(PressStation) forControlEvents:UIControlEventTouchUpInside];
        [self.vieOut addSubview:btn];
        
    }
    else{
        currentHight=44*arrTick.count+nextHight;
        if (currentHight+24>viewHight) {
            currentHight=viewHight-24;
        }
    }
    
     self.vieOut.frame=CGRectMake(self.vieOut.frame.origin.x, self.vieOut.frame.origin.y, self.vieOut.frame.size.width, currentHight);
    self.table.frame=CGRectMake(self.table.frame.origin.x, self.table.frame.origin.y, self.table.frame.size.width, currentHight-nextHight);
    [self.table reloadData];
    arr=Nil;

}
-(void)PressStation{
    TrainStation *jokecon=[[TrainStation alloc]init];
    
    NSString *str=[[[[self.arrReturn objectForKey:@"Links"] objectForKey:@"Link"] objectAtIndex:0] objectForKey:@"href"];
    
    //        NSLog(@"%@",str);
    NSRange range = [str rangeOfString:@"query="];
    jokecon.strKey=[str substringFromIndex:range.location +range.length];
    jokecon.isQuery=YES;
    jokecon.strClass=self.labTrainClass.text;
    str=nil;
    [ self.navigationController  pushViewController:jokecon animated:YES];
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark 异步数据请求
-(void)GetDatas{
    tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:self.strKey Start:0  Limit:4];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"allMessage"];
    
}
-(void)requestFinished:(ASIHTTPRequest*)request{
    
    NSString *response = [request responseString];
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
    
    if ([request.username isEqual:@"allMessage"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess==YES) {
            if (self.arrReturn==nil) {
                self.arrReturn=[[NSMutableDictionary alloc]init];
            }
            self.arrReturn =[[[[[[[Result objectForKey:@"Response"] objectForKey:@"Categories"] objectForKey:@"Category"] objectAtIndex:0]objectForKey:@"Items"] objectForKey:@"Item"]objectAtIndex:0];
            
            [self ShowData];
//            [self.table reloadData];
            
        }
    }
}

-(void)requestFailed:(ASIHTTPRequest*)request{
    
    
}


#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
        return arrTick.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[[arrTick objectAtIndex:indexPath.row] objectForKey:@"name"];
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 44)];
    lab.text=[[arrTick objectAtIndex:indexPath.row] objectForKey:@"text"];
    lab.textColor=[UIColor colorWithHexString:@"0x565656"];
    lab.font=[UIFont systemFontOfSize:14];
    cell.accessoryView=lab;
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    cell.indentationLevel = 1;
    cell.indentationWidth = 22.0f;
    cell.textLabel.textColor=[UIColor colorWithHexString:@"0x565656"];
        //    cell.textLabel.text=@"string";
    // Configure the cell...
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
